import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:mtf/R.dart';
import 'package:mtf/app/modules/video_detail_module/video_detail_controller.dart';
import 'package:mtf/app/theme/app_colors.dart';
import 'package:mtf/app/widget/AppBarLeftTitle.dart';
import 'package:video_player/video_player.dart';
import 'package:webview_flutter/webview_flutter.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class videoDetailPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _VideoAppState();
  }
}

class _VideoAppState extends State<videoDetailPage> {

  late WebViewController webController=WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000));
  late VideoPlayerController videoPlayerController;
  var controller=Get.put(videoDetailController());
  late ChewieController chewieController;
  @override
  Widget build(BuildContext context) {
    var title=Get.arguments["title"];
    var time=Get.arguments["time"];
    var cover=Get.arguments["cover"];
    var video=Get.arguments["video"];

    return Scaffold(
      appBar: AppBarLeftTitle(
        title: "详情",
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 10),
        child:Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 20),
              child: Text(title??"",
                  style: TextStyle(
                      color: c_333333,
                      fontSize: 18,
                      fontWeight: FontWeight.bold)),
            ),
            Container(
                margin: EdgeInsets.only(top: 5),
                child: Text(time??"",
                    style: TextStyle(color: c_999999, fontSize: 12))),
            Container(
              margin: EdgeInsets.only(top: 15),
              child: InkWell(
                onTap: (){
                  setState(() {
                    videoPlayerController.value.isPlaying
                        ? videoPlayerController.pause()
                        : videoPlayerController.play();
                  });
                },
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Container(
                        width: double.infinity,
                        height: 230,
                        color: c_bbbbbb,
                        child: Chewie(
                          controller: chewieController,
                        )
                    )
                  ],
                ),
              ),
            ),
            Expanded(
              child:WebViewWidget(
                controller: webController,
              ),
            )
          ],
        )
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(Get.arguments["video"]))
      ..initialize().then((_) {

      });
    videoPlayerController.setLooping(true);
    var content=Get.arguments["content"];
    setState(() {
      webController.loadHtmlString(content);
    });
    chewieController=ChewieController(videoPlayerController: videoPlayerController);
  }

  @override
  void dispose() {
    super.dispose();
    chewieController.pause();
    chewieController.dispose();
  }
}
